﻿using System.IO;
using Entity;
using Newtonsoft.Json;

namespace FastCopyWin
{
    internal class ConfigManagementService
    {
        private readonly ConfigData configData;

        private static readonly ConfigManagementService configManagementService = new();

        ConfigManagementService()
        {
            string currentPath = $"{System.Environment.CurrentDirectory}";
            configData = new()
            {
                MaxRecord = 500,
                DynamicDataFilePath = currentPath + "\\d.txt",
                ConstDataFilePath = currentPath + "\\c.txt",
                DynamicDataShortcutKey = new()
                {
                    "CTRL",
                    "M"
                },
                ConstDataShortcutKey = new()
                {
                    "CTRL",
                    "B"
                }
            };
            string configPath = currentPath + "\\config.txt";
            if (!File.Exists(configPath))
            {
                File.WriteAllText(configPath, JsonConvert.SerializeObject(configData));
            }
            else
            {
                string json = File.ReadAllText(configPath);
                ConfigData? tmp = JsonConvert.DeserializeObject<ConfigData>(json);
                if (tmp != null)
                {
                    configData = tmp;
                }
            }
        }

        public static ConfigManagementService GetInstance()
        {
            return configManagementService;
        }

        public ConfigData GetCurrentConfig()
        {
            return configData;
        }
    }
}
